package com.ns.scs.feedback.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.feedback.bo.FeedbackEntryBO;
import com.ns.scs.feedback.bo.FeedbackPageBO;
import com.ns.scs.feedback.dto.FeedBackPageDto;
import com.ns.scs.feedback.entry.FeedBackEntry;
import com.ns.scs.feedback.service.IFeedBackService;
import com.ns.scs.feedback.vo.FeedBackExtraVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 反馈相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/feedback")
@RequiredArgsConstructor
@CrossOrigin
public class FeedBackController {

    private final IFeedBackService service;


    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping
    @ReqLog(module = "反馈模块", name = "增", authFlag = false)
    @RateLimiter(time = 5, count = 1, limitType = LimitType.IP)
    public ResultOk add(@RequestBody FeedbackEntryBO bo) {
        long count = service.count();
        FeedBackEntry entry = new FeedBackEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setSort(count + 1);
        return ResultOk.getByBoolean(service.saveOrUpdate(entry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "反馈模块", name = "改")
    public ResultOk update(@RequestBody FeedbackEntryBO bo) {
        FeedBackEntry entry = new FeedBackEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.saveOrUpdate(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "反馈模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "反馈模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "反馈模块", name = "详情")
    public ResultOk get(Long id) {
        return ResultOk.getSuccessData(service.getById(id));
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "反馈模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody FeedbackPageBO bo) {
        FeedBackPageDto dto = new FeedBackPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<FeedBackExtraVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }
}
